﻿//#define USINGACCESSDB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using myDBDataAdapter = System.Data.SqlClient.SqlDataAdapter;
using myDBConnection = System.Data.SqlClient.SqlConnection;
using myDBTransaction = System.Data.SqlClient.SqlTransaction;
using myDBCommand = System.Data.SqlClient.SqlCommand;
using YNCNumberSys.db;
using YNCNumberSys.DbDal;
using YNCNumberSys.Entity;
using C1.Win.C1FlexGrid;
using AppUCtrol;

namespace YNCNumberSys
{
    /// <summary>
    /// xsl核查一遍 20221121.
    /// </summary>
    public partial class PapercodeQueryForm : Form
    {
        private string s_用户名;
        public string str_用户名
        {
            get { return s_用户名; }
            set {s_用户名 = value;}
        }

        private string s_机床;
        public string str_机床
        {
            get { return s_机床; }
            set { s_机床 = value; }
        }

        private string s_A号;
        public string str_A号
        {
            get { return s_A号; }
            set { s_A号 = value; }
        }
        bool m_changeFire = false;
        private void initComJCXH()
        {
            CodeDal codedal = new CodeDal();
            CodeEntity codeentity = codedal.GetCodeEntity();
            PaperCodeDal dal = new PaperCodeDal();
            PaperCodeEntity ent = new PaperCodeEntity();
            ent.Wx = null;
            ent.Zf = null;
            DataTable dt = dal.GetPaperCode(ent, codeentity);
            DataView dataView = dt.DefaultView;
            System.Data.DataTable dt_distinct = dataView.ToTable(true, "机床型号");//注：其中ToTable（）的第一个参数为是否DISTINCT
            cbx_机床型号.DisplayMember = "机床型号";
            var dr = dt_distinct.NewRow();
            dr["机床型号"] = "";
            dt_distinct.Rows.InsertAt(dr, 0);
            cbx_机床型号.DataSource = dt_distinct;
            cbx_机床型号.SelectedIndex = -1;
            if(!string.IsNullOrEmpty(s_机床))
            {
                bool bFind = false;
                DataTable dt2=(DataTable)cbx_机床型号.DataSource;
                for (int i=0;i< dt2.Rows.Count;i++ )
                {
                    if(s_机床== dt2.Rows[i][0].ToString())
                    {
                        cbx_机床型号.SelectedIndex = i;
                        bFind = true;
                        break;
                    }
                }
                bool bFindA = false;
                if (bFind)
                {
                    cbx_机床型号_SelectedIndexChanged(null, null);
                    DataTable dt3 = (DataTable)cbx_A号.DataSource;
                    for (int i = 0; i < dt3.Rows.Count; i++)
                    {
                        if (s_A号 == dt3.Rows[i][0].ToString())
                        {
                            cbx_A号.SelectedIndex = i;
                            bFindA = true;
                            break;
                        }
                    }
                }
                if(bFind && bFindA)
                {
                    btn_查询_Click(null,null);
                }

            }
        }
        public PapercodeQueryForm()
        {
            InitializeComponent();
            //initComJCXH();
        }
        FilterRow m_fr = null;
        private void btn_查询_Click(object sender, EventArgs e)
        {
            m_flex.BeginUpdate();
            m_flex.DataSource=null;
            m_flex.EndUpdate();
            CodeDal codedal = new CodeDal();
            CodeEntity codeentity=codedal.GetCodeEntity();
            PaperCodeDal dal = new PaperCodeDal();
            PaperCodeEntity ent = new PaperCodeEntity();
            ent.Wx = this.checkBox外协图号.Checked?1:0;
            ent.Zf = this.checkBox作废.Checked ? 1 : 0;
            ent.Zh = this.checkBox申请人.Checked ? s_用户名:string.Empty;
            ent.Jcxh = cbx_机床型号.Text != string.Empty ? cbx_机床型号.Text : string.Empty;
            ent.Ah= cbx_A号.Text != string.Empty ? cbx_A号.Text : string.Empty;
            DataTable dt = dal.GetPaperCode(ent, codeentity);
            m_flex.DataSource = dt;
            if (m_flex.Cols.Contains("外协")) m_flex.Cols["外协"].Visible = false;
            if (m_flex.Cols.Contains("外协单位")) m_flex.Cols["外协单位"].Visible = false;
            if (m_flex.Cols.Contains("作废")) m_flex.Cols["作废"].Visible = false;
            if (m_flex.Cols.Contains("图纸类型")) m_flex.Cols["图纸类型"].Visible = false;
            if (m_flex.Cols.Contains("ID")) m_flex.Cols["ID"].Visible = false;
            m_flex.BeginUpdate();
            for (int r = 1; r < m_flex.Rows.Count; r++) m_flex[r, 0] = r;
            m_flex.EndUpdate();
            labelDbRows.Text="结果:"+ dt.Rows.Count + "行";
            if(m_fr==null)
               m_fr = new FilterRow(m_flex);
        }
        private void btn_结果导出_Click(object sender, EventArgs e)
        {
            string str_Title = "导出到Excel";
            string str_DefaultName = "查询结果";
            string str_Extension = "xls; xlsx";
            string str_DialogName = "另存为";
            Autodesk.AutoCAD.Windows.SaveFileDialog.SaveFileDialogFlags sfFlags = Autodesk.AutoCAD.Windows.SaveFileDialog.SaveFileDialogFlags.DoNotTransferRemoteFiles;
            Autodesk.AutoCAD.Windows.SaveFileDialog ofd_选择文件 = new Autodesk.AutoCAD.Windows.SaveFileDialog(str_Title, str_DefaultName, str_Extension, str_DialogName, sfFlags);
            if (ofd_选择文件.ShowDialog() == DialogResult.OK)
            {
                string str_FileName = ofd_选择文件.Filename;
                try
                {
                    m_flex.SaveExcel(str_FileName, FileFlags.IncludeFixedCells);
                }
                catch (System.Exception ex)
                {
                    Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("导出失败！");
                }
            }
        }
        private void cbx_机床型号_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!m_changeFire) return;
            CodeDal codedal = new CodeDal();
            CodeEntity codeentity = codedal.GetCodeEntity();
            PaperCodeDal dal = new PaperCodeDal();
            PaperCodeEntity ent = new PaperCodeEntity();
            ent.Wx = 0;
            ent.Zf = 0;
            ent.Jcxh = cbx_机床型号.Text != string.Empty ? cbx_机床型号.Text : string.Empty;
            DataTable dt = dal.GetPaperCode(ent, codeentity);
            if (dt != null)
            {
                DataView dataView = dt.DefaultView;
                System.Data.DataTable dt_distinct = dataView.ToTable(true, "A号");
                cbx_A号.DisplayMember = "A号";
                var dr = dt_distinct.NewRow();
                dr["A号"] = "";
                dt_distinct.Rows.InsertAt(dr, 0);
                cbx_A号.DataSource = dt_distinct;
                cbx_A号.SelectedIndex = -1;
            }
        }

        private void PapercodeQueryForm_Load(object sender, EventArgs e)
        {
            m_changeFire = false;
            initComJCXH();
            m_changeFire = true;
        }

        private void textBoxTuhaoFilter_KeyPress(object sender, KeyPressEventArgs e)
        {
            //string s = ((TextBox)sender).Text;
            ////if (e.KeyChar == Keys.Delete )
            //{
            //    string cur = s + e.KeyChar;
            //    string exp=BuildFilterExpression(m_col, cur);

            //    m_flex.Cols[m_col].Filter.
            //}
            ////if (m_col >= 0)
            //    ;
        }
        int m_col = -1;
        private int GetCol(string colName)
        {
            for(int i=1;i<m_flex.Cols.Count;i++)
            {
                if (colName == m_flex.Cols[i].Caption)
                    return i;
            }
            return -1;
        }

        private string BuildFilterExpression(int col, string expr)
        {
            // operators we recognize
            string oper = "<>=";
            // no operators? use 'like' for strings, = for other types
            if (oper.IndexOf(expr[0]) < 0)
            {
                return (m_flex.Cols[col].DataType == typeof(string))
                    ? string.Format(" like '*{0}*'", expr)
                    : string.Format(" = '{0}'", expr);
            }
            // look for end of operators
            for (int index = 0; index < expr.Length; index++)
            {
                if (oper.IndexOf(expr[index]) < 0)
                {
                    string retval = expr.Substring(0, index) + " ";
                    retval += string.Format("'{0}'", expr.Substring(index).Trim());
                    return retval;
                }
            }
            // if we got here, the condition must be bad (e.g. ><)
            //Debug.WriteLine("Can't build filter expression.");
            return "";
        }

        private void buttonClearFilter_Click(object sender, EventArgs e)
        {
            if (m_fr != null)
                m_fr.Clear();
        }

        private void m_flex_ChangeEdit(object sender, EventArgs e)
        {
            labelDbRows.Text = "结果:" + m_fr.m_count + "行";
        }
    }
}
